﻿using System.Globalization;
using System.Xml.Linq;
using IcicleFramework.Components.Health;

namespace IcicleFramework.Components.Actions.Damage
{
    public class DamageAction : ImmediateAction
    {
        public float Damage { get; set; }

        protected override void Execute()
        {
            //Try to find the IHealthComponent for the target.
            if (Target != null)
            {
                IHealthComponent health = Target.GetComponent<IHealthComponent>();

                if (health != null)
                {
                    health.Damage(Damage, ParentGameObject);
                }
            }
            base.Execute();
        }

        public override void Deserialize(XElement element)
        {
            XElement damageElement = element.Element("damage");
            float damage = 0.0f;

            if (damageElement != null)
            {
                float.TryParse(damageElement.Value, NumberStyles.Float, CultureInfo.InvariantCulture, out damage);
            }

            Damage = damage;

            base.Deserialize(element);
        }
    }
}
